package com.fishingwithme.android.navigation

import androidx.compose.runtime.Composable
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.fishingwithme.android.navigation.FishingNavigation.CIRCLE_DETAIL
import com.fishingwithme.android.ui.screens.activities.ActivitiesScreen
import com.fishingwithme.android.ui.screens.anybitting.AnyBittingScreen
import com.fishingwithme.android.ui.screens.auth.LoginScreen
import com.fishingwithme.android.ui.screens.auth.RegisterScreen
import com.fishingwithme.android.ui.screens.circle.AddCircleScreen
import com.fishingwithme.android.ui.screens.circle.CircleDetailScreen
import com.fishingwithme.android.ui.screens.circle.CircleManageScreen
import com.fishingwithme.android.ui.screens.circle.CircleScreen
import com.fishingwithme.android.ui.screens.harvest.AddHarvestScreen
import com.fishingwithme.android.ui.screens.harvest.HarvestApproveScreen
import com.fishingwithme.android.ui.screens.harvest.HarvestListScreen
import com.fishingwithme.android.ui.screens.harvest.HarvestShowScreen
import com.fishingwithme.android.ui.screens.home.HomeScreen
import com.fishingwithme.android.ui.screens.home.NavigatorScreen
import com.fishingwithme.android.ui.screens.map.screens.MapSelectionScreen
import com.fishingwithme.android.ui.screens.map.screens.SpotNavUpdateScreen
import com.fishingwithme.android.ui.screens.my.MyScreen
import com.fishingwithme.android.ui.screens.my.maintain.ActivitySettingScreen
import com.fishingwithme.android.ui.screens.my.maintain.AuditScreen
import com.fishingwithme.android.ui.screens.my.maintain.CreditActionsScreen
import com.fishingwithme.android.ui.screens.my.maintain.FisherCenter
import com.fishingwithme.android.ui.screens.my.maintain.MyHarvestMaintainScreen
import com.fishingwithme.android.ui.screens.my.maintain.MySpotMaintainScreen
import com.fishingwithme.android.ui.screens.my.maintain.ToBeCircleMaintainerScreen
import com.fishingwithme.android.ui.screens.my.maintain.ToBeSpotMaintainerScreen
import com.fishingwithme.android.ui.screens.my.points.CreditLevelScreen
import com.fishingwithme.android.ui.screens.my.points.CreditRecordScreen
import com.fishingwithme.android.ui.screens.my.points.CreditRulesScreen
import com.fishingwithme.android.ui.screens.my.settings.AgreementPolicyScreen
import com.fishingwithme.android.ui.screens.my.settings.SettingsScreen
import com.fishingwithme.android.ui.screens.my.settings.ThemeSettingsScreen
import com.fishingwithme.android.ui.screens.my.settings.UserEditScreen
import com.fishingwithme.android.ui.screens.spots.AddSpotScreen
import com.fishingwithme.android.ui.screens.spots.SpotsScreen
import com.fishingwithme.android.ui.screens.spots.UpdateSpotScreen

object FishingNavigation {
    const val LOGIN = "login"
    const val REGISTER = "register"
    const val ANYBITTING = "anybitting"
    const val ACTIVITIES = "activities"
    const val ACTIVITIES_SETTINGS = "activity_settings"
    const val SPOTS = "spots"
    const val UPDATE_SPOT = "update"
    const val UPDATE_CIRCLE = "update_circle"
    const val MAP_SPOT_NAV_UPDATE = "spots/update/nav"
    const val ADD_SPOT = "spots/addspot"

    const val HOME = "home"
    const val CIRCLE_DETAIL = "circle/detail"
    const val APPROVE_SPOT = "approve_spot"
    const val APPROVE_HARVEST = "approve_harvest"
    const val MY = "my"
    const val NAVIGATOR = "home/navigator"
    const val MAP_SELECTION = "map/selection"
    const val USER_SETTINGS = "my/user-settings"
    const val USER_EDIT = "my/user-edit"
    const val CREDIT_LVL = "my/credit-lvl"
    const val CREDIT_RECORD = "my/credit-record"
    const val CREDIT_RULES = "my/credit-rules"
    const val SHOW = "show"
    const val AGREEMENT_POLICY = "agrpp"

    const val THEME_SETTINGS = "theme-settings"
    const val MAINTAINER_CENTER = "maintainer_center"
    const val TO_BE_CIRCLE_MAINTAINER = "to_be_circle_maintainer"
    const val CIRCLE_MGR = "circle_mgr"
    const val CIRCLE_SETTINGS = "circle_settings"
    const val POINTS_SETTINGS = "points_settings"
    const val SPOT_APPROVE = "spot_approve"
    const val HARVEST_APPROVE = "harvest_approve"
    const val TO_BE_SPOT_MAINTAINER = "to_be_spot_maintain"
    const val AUDITOR_APPROVE = "auditor_approve"
    const val HARVEST = "spots/harvest"
    const val HARVEST_FROM_SPOT = "spots/harvest_from_spot"
    const val HARVEST_LIST = "harvest/list" // New route for harvest list
    const val ADD_CIRCLE = "add_circle"
}

@Composable
fun FishingNavHost(
    navController: NavHostController = rememberNavController(),
    startDestination: String = FishingNavigation.ANYBITTING
) {
    NavHost(
        navController = navController,
        startDestination = startDestination
    ) {
        composable(FishingNavigation.LOGIN) {
            LoginScreen(_navController = navController)
        }

        composable(FishingNavigation.REGISTER) {
            RegisterScreen(navController = navController)
        }

        composable(FishingNavigation.ANYBITTING) {
            AnyBittingScreen(navController = navController)
        }

        composable(FishingNavigation.ACTIVITIES) {
            ActivitiesScreen(navController = navController)
        }

        composable(FishingNavigation.SPOTS) {
            SpotsScreen(navController = navController)
        }

        composable(FishingNavigation.ADD_SPOT) {
            AddSpotScreen(navController = navController)
        }

        composable(FishingNavigation.MY) {
            MyScreen(navController = navController)
        }

        // Settings screen
        composable(FishingNavigation.USER_SETTINGS) {
            SettingsScreen(navController = navController)
        }

        // User edit screen
        composable(FishingNavigation.USER_EDIT) {
            UserEditScreen(navController = navController)
        }

        // Agreement and Privacy Policy screen
        composable("${FishingNavigation.AGREEMENT_POLICY}?type={type}") { backStackEntry ->
            val type = backStackEntry.arguments?.getString("type") ?: "AD_UA"
            AgreementPolicyScreen(navController = navController, type = type)
        }

        // Home related screens
        composable("${FishingNavigation.HOME}?id={id}") { backStackEntry ->
            val spotId = backStackEntry.arguments?.getString("id") ?: ""
            HomeScreen(navController = navController, id = spotId)
        }

        // Home related screens
        composable("${FishingNavigation.APPROVE_SPOT}?id={id}&approve={approve}") { backStackEntry ->
            val spotId = backStackEntry.arguments?.getString("id") ?: ""
            val approve = backStackEntry.arguments?.getString("approve") ?: ""
            HomeScreen(navController = navController, id = spotId, approve = approve)
        }

        // Update related screens
        composable("${FishingNavigation.UPDATE_SPOT}?id={id}") { backStackEntry ->
            val spotId = backStackEntry.arguments?.getString("id") ?: ""
            UpdateSpotScreen(navController = navController, id = spotId)
        }

        composable("${FishingNavigation.NAVIGATOR}?id={id}&moduleKey={moduleKey}") { backStackEntry ->
            val spotId = backStackEntry.arguments?.getString("id") ?: ""
            val moduleKey = backStackEntry.arguments?.getString("moduleKey") ?: ""
            NavigatorScreen(spotId = spotId, moduleKey)
        }


        // Add this composable for the harvest form
        composable(FishingNavigation.HARVEST) { backStackEntry ->
            AddHarvestScreen(
                navController = navController
            )
        }

        composable("${FishingNavigation.HARVEST_FROM_SPOT}?spotId={spotId}") { backStackEntry ->
            val spotId:String? = backStackEntry.arguments?.getString("spotId")
            AddHarvestScreen(
                navController = navController,
                spotId = spotId
            )
        }

        composable(FishingNavigation.ADD_CIRCLE) { backStackEntry ->
            AddCircleScreen(
                navController = navController
            )
        }

        // Harvest list screen
        composable("${FishingNavigation.HARVEST_LIST}?spotId={spotId}") { backStackEntry ->
            val spotId = backStackEntry.arguments?.getString("spotId") ?: ""
            HarvestListScreen(navController = navController, spotId = spotId)
        }


        // Map selection screen
        composable("${FishingNavigation.MAP_SELECTION}?moduleKey={moduleKey}") { backStackEntry ->
            val moduleKey = backStackEntry.arguments?.getString("moduleKey") ?: ""
            MapSelectionScreen(
                navController = navController,
                moduleKey = moduleKey
            )
        }

        composable("${FishingNavigation.MAP_SPOT_NAV_UPDATE}?moduleKey={moduleKey}&spotId={spotId}") { backStackEntry ->
            val moduleKey = backStackEntry.arguments?.getString("moduleKey") ?: ""
            val spotId = backStackEntry.arguments?.getString("spotId") ?: ""
            SpotNavUpdateScreen(
                navController = navController,
                moduleKey = moduleKey,
                spotId = spotId
            )
        }

        composable("${FishingNavigation.CREDIT_LVL}?lvl={lvl}") { backStackEntry ->
            val lvl: Int = backStackEntry.arguments?.getString("lvl")?.toInt() ?: 1
            CreditLevelScreen(navController = navController, lvl)
        }

        composable(FishingNavigation.CREDIT_RECORD) {
            CreditRecordScreen(navController = navController)
        }

        composable(FishingNavigation.CREDIT_RULES) {
            CreditRulesScreen(navController = navController)
        }

        composable("${FishingNavigation.SHOW}?id={id}") { backStackEntry ->
            val id = backStackEntry.arguments?.getString("id") ?: ""
            HarvestShowScreen(navController = navController, harvestId = id)
        }

        composable("${FishingNavigation.APPROVE_HARVEST}?id={id}&approve={approve}") { backStackEntry ->
            val id = backStackEntry.arguments?.getString("id") ?: ""
            HarvestApproveScreen(navController = navController, harvestId = id)
        }


        composable(FishingNavigation.THEME_SETTINGS) {
            ThemeSettingsScreen(navController = navController)
        }

        composable(FishingNavigation.MAINTAINER_CENTER) {
            FisherCenter(navController = navController)
        }
        composable(FishingNavigation.SPOT_APPROVE) {
            MySpotMaintainScreen(navController = navController)
        }

        composable(FishingNavigation.HARVEST_APPROVE) {
            MyHarvestMaintainScreen(navController = navController)
        }

        composable(FishingNavigation.AUDITOR_APPROVE) {
            AuditScreen(navController = navController)
        }
        composable(FishingNavigation.POINTS_SETTINGS) {
            CreditActionsScreen(navController = navController)
        }
        composable(FishingNavigation.TO_BE_CIRCLE_MAINTAINER) {
            ToBeCircleMaintainerScreen()
        }

        composable(FishingNavigation.TO_BE_SPOT_MAINTAINER) {
            ToBeSpotMaintainerScreen()
        }

        composable(FishingNavigation.CIRCLE_SETTINGS) {
            CircleScreen(navController = navController)
        }
        composable(FishingNavigation.ACTIVITIES_SETTINGS) {
            ActivitySettingScreen()
        }

        composable("${CIRCLE_DETAIL}?id={id}") { backStackEntry ->
            val id = backStackEntry.arguments?.getString("id") ?: ""
            CircleDetailScreen(navController, id)
        }
        composable("${FishingNavigation.CIRCLE_MGR}?id={id}") { backStackEntry ->
            val id = backStackEntry.arguments?.getString("id") ?: ""
            CircleManageScreen(navController = navController, id)
        }
    }
}